# Load Data
tab.path <- "results/table/"
tab.name <- "app_tab_d03.tex"

RegDatLong_soc <- readRDS("data/natdata_nuwsssocs.RDS")
RegDatLong_anti <- readRDS("data/natdata_antisuff.RDS")


# Regressions

konts <- c( "log_pop" , "SR",
         "AV_AGE_F",   "SC1" , "TFR" ,
         "F_SMAM" , 
         "HC1" ,"HC2" ,  "HC4", "HC5",
         "SINGLE_PER" ,"F_CEL_4554", "M_CEL_4554", "FMAR_PRATE",
         "ECMR","ACRES", "POP_DENS" )

vars <- c( paste0("factor(dpost)*", "march_path"),"factor(dpost)" ,"march_path")
outvars <- c( "^factor.dpost.TRUE.march_pathTRUE$","^factor.dpost.TRUE$" ,"^march_pathTRUE$")
labvars <- c("DPost X March","DPost", "March")

m1 <-felm(as.formula(RegFor( y = "society", x = c(vars, "roman_road") ,
      FE = "county_name" , IV="0", clust = "county_name" )),
           data= RegDatLong_soc)
m2 <- felm(as.formula(RegFor( y = "society", x = c(vars, konts) ,
      FE = "county_name" , IV="0", clust = "county_name" )),
           data= RegDatLong_soc)
m3 <-felm(as.formula(RegFor( y = "society", x = c(vars, konts, "roman_road") ,
      FE = "county_name" , IV="0", clust = "county_name" )),
           data= RegDatLong_anti)
m4 <- felm(as.formula(RegFor( y = "society", x = c(vars, "roman_road") ,
      FE = "county_name" , IV="0", clust = "county_name" )),
           data= RegDatLong_anti)

### Export 
regstoadd <- list(m1,m2, m3,m4)


MSd <- function(variable){
  x1 <- round(mean(variable, na.rm = TRUE), 2)
  x2<-  round(sd(variable, na.rm = TRUE), 2)
  return(c(x1,x2))
}

Stats <- rbind(
  MSd(RegDatLong_soc$society),
  MSd(RegDatLong_soc$society),
  MSd(RegDatLong_anti$society),
  MSd(RegDatLong_anti$society)
)

####OUT
add.lines <- list(AddLines(3, "County FE", rep(LMc("Yes"),6)),
                AddLines( 3, "Controls", c( rep(c(LMc("No"), LMc("Yes")) ,2))),
                AddLines( 3, "Mean dep. var." , Stats[,1] ),
                AddLines( 3, "Sd dep. var." , Stats[,2] )
                 )

fileConn<-file(paste0(tab.path,tab.name))
print(fileConn)
writeLines(stargazer(regstoadd,
                     float = FALSE ,
                     keep= outvars,
                     order = outvars ,
                     covariate.labels= labvars ,
                     star.cutoffs = c(0.1099, 0.0599, 0.0199) ,
                     digits.extra = 0,
                     multicolumn=F,#
                    #  column.sep.width="10pt",
                     column.labels = c("NUWSS Branch", "Anti-Suffrage Branch"),
                     column.separate = c(2,2),
                    #  dep.var.caption = paste("Number of Strikes" ),
                     dep.var.labels.include = F,
                     font.size = "footnotesize",
                     omit.table.layout = "n",
                     align =T,
                     add.lines = add.lines,
                     digits = 3,
                     intercept.top = T,
                     intercept.bottom = F,
                     keep.stat = c("rsq" , "n") ,
                     omit.stat = c("res.dev","ser") ),
           fileConn)
close(fileConn)
rm(list=
    setdiff(ls()[sapply(ls(), function(x) any(class(get(x)) == 'data.frame'))],
    c("Re","Ind")))